/**
 * @fileOverview Contains Debugger interface for Firebug and Safari
 */

import  {IConsoleLogger, LOG_LEVEL } from "./IConsoleLogger";

/*exported JSJaCConsoleLogger */
/*global console */

/**
 * A logger that logs using the 'console' object.
 * @constructor
 * @class Implementation of the Debugger interface for 
 * {@link http://www.getfirebug.com/ | Firebug} and Safari.
 * Creates a new debug logger to be passed to jsjac's connection
 * constructor. Of course you can use it for debugging in your code
 * too.
 * @extends {JSJaCDebugger}
 * @author Stefan Strigler steve@zeank.in-berlin.de
 * @param {int} level The maximum level for debugging messages to be
 * displayed. Thus you can tweak the verbosity of the logger. A value
 * of 0 means very low traffic whilst a value of 4 makes logging very
 * verbose about what's going on.
 */
export  class JSJaCConsoleLogger implements IConsoleLogger {
  private static level:number;
  private hasInfoMethod:boolean=true;
  private hasConsole=true
  constructor(level:number=LOG_LEVEL.DEBUG){
     JSJaCConsoleLogger.level=level
     if(typeof(console)=='undefined'){
       this.hasConsole=false
       this.hasInfoMethod=false
     }
     if(typeof(console.warn)=='undefined' ||
      typeof(console.error)=='undefined' ||
      typeof(console.info) =='undefined' ||
      typeof(console.debug)=='undefined'){
       this.hasInfoMethod=false

     }
  }
  info(msg: string): void {
    if(JSJaCConsoleLogger.level<LOG_LEVEL.INFO)
      return 
    console.info(msg)
  }
  debug(msg: string): void {
    if(JSJaCConsoleLogger.level<LOG_LEVEL.DEBUG)
      return 
    console.debug(msg)
  }
  warn(msg: string): void {
    if(JSJaCConsoleLogger.level<LOG_LEVEL.WARN)
      return 
    console.warn(msg)
  }
  error(msg: string): void {
    if(JSJaCConsoleLogger.level<LOG_LEVEL.ERROR)
      return 
    console.error(msg)
  }
  setLevel(level: number): IConsoleLogger {
    JSJaCConsoleLogger.level=level;
    return this;
  }
  getLevel(): number {
    return JSJaCConsoleLogger.level;
  }
  log(msg: string, level: number=5): void {
    if (level > JSJaCConsoleLogger.level)
      return;
    if(!this.hasConsole )
     return 
     if(!this.hasInfoMethod){
       console.log(msg)
       return 
     }
    try {
      switch (level) {
      case 0:
        console.error(msg);
        break;
      case 1:
        console.warn(msg);
        break;
      case 2:
        console.info(msg);
        break;
      case 4:
        console.debug(msg);
        break;
      default:
        console.log(msg);
        break;
      }
    } catch(e1) { try { console.log(msg); } catch(e2) {} }
  }
}